﻿using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using TestsModel.Data;
using TestsModel.Enums;
using TestsModel.Parsers;

namespace TestUp.Models.Helpers
{
    public class TestHelper
    {
        public TestData LoadTest(int id)
        {
            var path = GetTestPath(id);
            if (File.Exists(path))
            {
                return XmlTestParser.ParseXmlFile(XDocument.Load(path));
            }
            return null;
        }

        public Test GetTest(int id)
        {
            return DataWorkItem.Entity.Tests.FirstOrDefault(x => x.TestId == id);
        }

        public void CreateTest(Test test, Stream file, TestFileTypes fileType)
        {
            if (test != null && file != null)
            {
                var course = DataHelpers.CourseHelper.GetCourse(test.CourseId);
                XDocument xDocument = null;
                string schemaLocation = HttpContext.Current.Server.MapPath("/App_Data/testTemplate.xsd");

                switch (fileType)
                {
                    case TestFileTypes.Xml:
                        {
                            //xmlDocument = new XmlDocument();
                            //xmlDocument.Load(file);
                            xDocument = XDocument.Load(file);//new XmlFileParser().Parse(xmlDocument, schemaLocation);
                            break;
                        }
                }
                if (xDocument != null && course != null)
                {
                    CreateTestEntity(test);
                    CreateTestFile(course, xDocument, test.TestName);
                }
            }
        }

        private void CreateTestEntity(Test sourceTest)
        {
            DataWorkItem.Entity.Tests.Add(new Test
                {
                    CourseId = sourceTest.CourseId,
                    TestName = sourceTest.TestName
                });
            DataWorkItem.Entity.SaveChanges();
        }

        private void CreateTestFile(Course course, XDocument xDocument, string testName)
        {
            var path = Path.Combine(CourseHelper.CoursesDirectory, course.CourseName, testName + ".xml");
            xDocument.Save(path);
        }

        public void UpdateTest(Test newTest)
        {
            var originalTest = GetTest(newTest.TestId);
            if (originalTest != null)
            {
                if (originalTest.TestName != newTest.TestName || originalTest.CourseId != newTest.CourseId)
                {
                    var testPath = GetTestPath(originalTest);
                    if (File.Exists(testPath))
                    {
                        File.Move(testPath, GetTestPath(newTest));
                    }

                    originalTest.CourseId = newTest.CourseId;
                    originalTest.TestName = newTest.TestName;
                    DataWorkItem.Entity.SaveChanges();
                }
            }
        }

        public string GetTestPath(int testId)
        {
            var test = GetTest(testId);
            string result = string.Empty;
            if (test != null)
                result = Path.Combine(CourseHelper.CoursesDirectory, test.Course.CourseName, test.TestName + ".xml");
            return result;
        }

        public string GetTestPath(Test test)
        {
            string result = string.Empty;
            if (test != null)
                result = Path.Combine(DataHelpers.CourseHelper.GetCourseDirectoryPath(test.CourseId), test.TestName + ".xml");
            return result;
        }

        private void DeleteTestFile(Test test)
        {
            string path = GetTestPath(test);
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }

        public void DeleteTest(int testId)
        {
            var test = GetTest(testId);
            if (test != null)
            {
                DataWorkItem.Entity.Tests.Remove(test);
                DataWorkItem.Entity.SaveChanges();
                DeleteTestFile(test);
            }
        }
    }
}